package com.funquer.cms.service.impl;

import com.funquer.admin.pageModel.DataGrid;
import com.funquer.cms.model.ChanneltypeEntity;
import com.funquer.cms.pageModel.ChanneltypePage;
import com.funquer.cms.service.ChanneltypeServiceI;
import com.funquer.dao.BaseDaoI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-17
 * Time: 下午12:24
 * To change this template use File | Settings | File Templates.
 */
@Service("channeltypeService")
public class ChanneltypeServiceImpl implements ChanneltypeServiceI {


    private BaseDaoI<ChanneltypeEntity> channeltypeDao;

    public BaseDaoI<ChanneltypeEntity> getChanneltypeDao() {
        return channeltypeDao;
    }

    @Autowired
    public void setChanneltypeDao(BaseDaoI<ChanneltypeEntity> channeltypeDao) {
        this.channeltypeDao = channeltypeDao;
    }

    @Override
    public DataGrid datagrid(ChanneltypePage channeltypePage) {
        DataGrid dg = new DataGrid();
        String hql="FROM ChanneltypeEntity";
        Map<String, Object> params = new HashMap<String, Object>();
        hql = addWhere(channeltypePage, hql, params);
        String totalHql = "select count(*) FROM ChanneltypeEntity";
        Map<String, Object> params2 = new HashMap<String, Object>();
        totalHql = addWhere(channeltypePage, totalHql, params2);
        hql = addOrder(channeltypePage, hql);
        List<ChanneltypeEntity> l = channeltypeDao.find(hql, params, channeltypePage.getPage(), channeltypePage.getRows());
        dg.setTotal(channeltypeDao.count(totalHql, params));
        dg.setRows(l);
        return dg;
    }


    private String addOrder(ChanneltypePage channeltypePage, String hql) {
        if (channeltypePage.getSort() != null) {
            hql += " order by " + channeltypePage.getSort() + " " + channeltypePage.getOrder();
        }
        return hql;
    }

    private String addWhere(ChanneltypePage channeltypePage, String hql, Map<String, Object> params) {
        hql += " where 1=1 ";
        if (channeltypePage.getChannelTypeName() != null && !channeltypePage.getChannelTypeName().trim().equals("")) {
            hql += " and typename like '%%" + channeltypePage.getChannelTypeName().trim() + "%%'";
        }
        return hql;
    }

    @Override
    public ChanneltypeEntity save(ChanneltypeEntity channeltypeEntity) {
        if(channeltypeEntity!=null){
            channeltypeEntity.setId(UUID.randomUUID().toString());
        }
        channeltypeDao.save(channeltypeEntity);
        return channeltypeEntity;
    }

    @Override
    public void remove(String ids) {
        for (String id : ids.split(",")) {
            ChanneltypeEntity t = channeltypeDao.get(ChanneltypeEntity.class, id);
            if (t != null) {
                channeltypeDao.delete(t);
            }
        }
    }

    @Override
    public ChanneltypeEntity getOne(String id) {
        ChanneltypeEntity t = channeltypeDao.get(ChanneltypeEntity.class, id);
        if (t != null) {
            return t;
        }
        return null;
    }

    @Override
    public ChanneltypeEntity edit(ChanneltypeEntity channeltypeEntity) {
        channeltypeDao.update(channeltypeEntity);
        return channeltypeEntity;
    }
}
